﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint.Administration;

using System.Net.Mail;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;

namespace MFD.SharePoint.Notifications
{
    
    public class Notification
    {
        //Get the email settings from the farm
        public static string emailserver = SPAdministrationWebApplication.Local.OutboundMailServiceInstance.Server.Address;
        public static string from = SPAdministrationWebApplication.Local.OutboundMailSenderAddress;

        public static void SendEmail(string to, EmailTemplate template, object values)
        {
            SendEmail(to, string.Empty, string.Empty, template, values);
        }

        public static void SendEmail(string to, EmailTemplate template, object values, MailPriority priority, string replyToAddress)
        {
            SendEmail(to, string.Empty, string.Empty, template, values, priority, replyToAddress);
        }

        private static void SendEmail(string to, string cc, string bcc, EmailTemplate template, object values, MailPriority priority, string replyToAddress)
        {
            EmailItem item = EmailItem.Create(template, values);

            item.To = to;
            item.CC = cc;
            item.BCC = bcc;
            item.Priority = priority;
            item.ReplyToAdress = replyToAddress;
            Send(item);
        }

        public static void SendEmail(string to, string cc, string bcc, EmailTemplate template, object values)
        {
            EmailItem item = EmailItem.Create(template, values);

            item.To = to;
            item.CC = cc;
            item.BCC = bcc;
            item.Priority = MailPriority.Normal;
            Send(item);
        }

        public static void SendEmail(string to, EmailTemplate template, List<object> values)
        {
            SendEmail(to, string.Empty, string.Empty, template, values);
        }

        public static void SendEmail(string to,string cc, string bcc, EmailTemplate template, List<object> values)
        {
            EmailItem item = EmailItem.Create(template, values);

            item.To = to;
            item.CC = cc;
            item.BCC = bcc;
            item.Priority = MailPriority.Normal;
            Send(item);
            
            
        }

        private static void Send(EmailItem item)
        {
            SmtpClient smtp = new SmtpClient(emailserver);

            MailMessage msg = item.GetEmailMessage();
            msg.From = new MailAddress(from);
            if (!string.IsNullOrEmpty(item.ReplyToAdress))
                msg.ReplyTo = new MailAddress(item.ReplyToAdress);
            smtp.Send(msg);
        }

        public static void SendEmail(SPWeb web,string to, EmailTemplate template, List<object> values)
        {
            EmailItem item = EmailItem.Create(template, values);

            SPUtility.SendEmail(web, true, false, to, item.Subject, item.Body);
        }

        /// <summary>
        /// Gets the users email addreses from a SharePoint Group
        /// </summary>
        /// <param name="group">SharePoint Group from where to extract the current user email addesses</param>
        /// <returns>A comma separated list of email addresses</returns>
        public static string GetEmails(SPGroup group)
        {
            string ret = null;
            foreach (SPUser user in group.Users)
            {
                if (user.Email == null || user.Email.Equals(string.Empty))
                    continue;
                if (ret == null)
                    ret = user.Email;
                else
                    ret = ret + ";" + user.Email;
            }

            return ret;
        }
        


    }
}
